package top.techqi.hamtools.func.diary

import android.app.Activity
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.view.Menu
import android.view.MenuItem
import android.widget.SeekBar
import kotlinx.android.synthetic.main.activity_diary_edit.*
import top.techqi.droidbdf.pangu.base.BaseActivity
import top.techqi.hamtools.R
import top.techqi.hamtools.db.assist.FreqUnit
import top.techqi.hamtools.db.assist.RstR
import top.techqi.hamtools.db.assist.RstS
import top.techqi.hamtools.db.assist.RstT
import top.techqi.hamtools.db.model.DiaryModel

class DiaryEditActivity : BaseActivity(), SeekBar.OnSeekBarChangeListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_diary_edit)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
    }

    override fun onContentChanged() {
        super.onContentChanged()

        unit_freq.setSelection(2)

        bindSeekBar(rmt_r_seek, this, 5)
        bindSeekBar(rmt_s_seek, this, 9)
        bindSeekBar(rmt_t_seek, this, 0)
        bindSeekBar(lct_r_seek, this, 5)
        bindSeekBar(lct_s_seek, this, 9)
        bindSeekBar(lct_t_seek, this, 0)
    }

    override fun onBackPressed() {
        if (!sign.text.isEmpty() || !mode.text.isEmpty()
                || !freq.text.isEmpty() || !power.text.isEmpty()
                || !locate.text.isEmpty() || !remote.text.isEmpty()
                || !note.text.isEmpty()) {
            val builder = AlertDialog.Builder(this)
            builder.setTitle(title)
            builder.setMessage(R.string.dialog_confirm_message)
            builder.setNegativeButton(android.R.string.cancel, null)
            builder.setPositiveButton(android.R.string.ok) { _, _ ->
                setResult(Activity.RESULT_CANCELED);finish()
            }
            builder.show()
        } else super.onBackPressed()
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.check, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> {
                onBackPressed()
                return true
            }
            R.id.check -> {
                if (checkAndSaveModel()) {
                    toast(R.string.diary_save_success)
                    setResult(Activity.RESULT_OK)
                    finish()
                }
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onStartTrackingTouch(seekBar: SeekBar) {
        // Do nothing
    }

    override fun onStopTrackingTouch(seekBar: SeekBar) {
        // Do nothing
    }

    override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
        when (seekBar) {
            rmt_r_seek -> {
                rmt_r_desc.text = resources.getStringArray(R.array.rst_r)[progress]
                rmt_r_value.text = "$progress"
            }
            rmt_s_seek -> {
                rmt_s_desc.text = resources.getStringArray(R.array.rst_s)[progress]
                rmt_s_value.text = "$progress"
            }
            rmt_t_seek -> {
                rmt_t_desc.text = resources.getStringArray(R.array.rst_t)[progress]
                rmt_t_value.text = "$progress"
            }
            lct_r_seek -> {
                lct_r_desc.text = resources.getStringArray(R.array.rst_r)[progress]
                lct_r_value.text = "$progress"
            }
            lct_s_seek -> {
                lct_s_desc.text = resources.getStringArray(R.array.rst_s)[progress]
                lct_s_value.text = "$progress"
            }
            lct_t_seek -> {
                lct_t_desc.text = resources.getStringArray(R.array.rst_t)[progress]
                lct_t_value.text = "$progress"
            }
        }
    }

    private fun bindSeekBar(seekBar: SeekBar, listener: SeekBar.OnSeekBarChangeListener, progress: Int) {
        seekBar.progress = progress
        seekBar.setOnSeekBarChangeListener(listener)
        listener.onProgressChanged(seekBar, progress, false)
    }

    private fun checkAndSaveModel(): Boolean {
        val model = DiaryModel()
        with(sign.text.toString()) {
            if (isEmpty()) {
                sign.requestFocus()
                toast(R.string.diary_check_sign)
                return false
            } else model.sign = this
        }
        with(mode.text.toString()) {
            if (isEmpty()) {
                mode.requestFocus()
                toast(R.string.diary_check_mode)
                return false
            } else model.mode = this
        }
        with(freq.text.toString()) {
            if (isEmpty()) {
                freq.requestFocus()
                toast(R.string.diary_check_freq)
                return false
            } else {
                toFloatOrNull().let {
                    if (it == null) return false
                    model.freq = it
                }
            }
        }
        with(power.text.toString()) {
            if (isEmpty()) {
                power.requestFocus()
                toast(R.string.diary_check_power)
                return false
            } else {
                toFloatOrNull().let {
                    if (it == null) return false
                    model.power = it
                }
            }
        }
        model.datetime = datetime.time
        model.freqUnit = FreqUnit.valueOf(unit_freq.selectedItemPosition)

        model.lctR = RstR.valueOf(lct_r_seek.progress)
        model.lctS = RstS.valueOf(lct_s_seek.progress)
        model.lctT = RstT.valueOf(lct_t_seek.progress)

        model.rmtR = RstR.valueOf(rmt_r_seek.progress)
        model.rmtS = RstS.valueOf(rmt_s_seek.progress)
        model.rmtT = RstT.valueOf(rmt_t_seek.progress)

        model.locate = locate.text.toString()
        model.remote = remote.text.toString()
        model.note = note.text.toString()

        return model.save()
    }
}
